/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo.international;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author VANDH
 */
public class Order extends javax.swing.JFrame {

    private Locale lc;
    private ResourceBundle rb;

    /**
     * Creates new form Order
     */
    public Order() {
        initComponents();

        cboLanguage.setSelectedIndex(0);
        changeLanguage();
    }

    private void changeLanguage() {
        if (cboLanguage.getSelectedIndex() == 0) {
            lc = new Locale("en", "US");
        } else if (cboLanguage.getSelectedIndex() == 1) {
            lc = new Locale("vi", "VN");
        }

        rb = ResourceBundle.getBundle("demo/international/order", lc);

        String keyDate = rb.getString("date");
        lblOrderDate.setText(keyDate);

        String keyProduct = rb.getString("product");
        lblProductName.setText(keyProduct);

        lblQuanlity.setText(rb.getString("quantity"));
        lblPrice.setText(rb.getString("price"));
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
     * content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblProductName = new javax.swing.JLabel();
        txtProductName = new javax.swing.JTextField();
        lblQuanlity = new javax.swing.JLabel();
        txtQuanlity = new javax.swing.JTextField();
        lblPrice = new javax.swing.JLabel();
        txtPrice = new javax.swing.JTextField();
        btnPayment = new javax.swing.JButton();
        lblOrderDate = new javax.swing.JLabel();
        txtOrderDate = new javax.swing.JTextField();
        cboLanguage = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaSummary = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblProductName.setText("Product");

        lblQuanlity.setText("Quanlity");

        lblPrice.setText("Price");

        btnPayment.setText("Payment");
        btnPayment.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPaymentActionPerformed(evt);
            }
        });

        lblOrderDate.setText("Date");

        cboLanguage.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "English", "Tiếng Việt" }));
        cboLanguage.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboLanguageItemStateChanged(evt);
            }
        });

        txaSummary.setEditable(false);
        txaSummary.setColumns(20);
        txaSummary.setLineWrap(true);
        txaSummary.setRows(5);
        jScrollPane1.setViewportView(txaSummary);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblOrderDate)
                        .addGap(18, 18, 18)
                        .addComponent(txtOrderDate, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(53, 53, 53)
                        .addComponent(cboLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnPayment, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(lblProductName)
                                    .addComponent(lblQuanlity)
                                    .addComponent(lblPrice))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtProductName, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtQuanlity, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblOrderDate)
                    .addComponent(txtOrderDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboLanguage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblProductName)
                    .addComponent(txtProductName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtQuanlity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblQuanlity))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPrice))
                .addGap(9, 9, 9)
                .addComponent(btnPayment)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPaymentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPaymentActionPerformed
        int quanlity = Integer.parseInt(txtQuanlity.getText());
        double price = Double.parseDouble(txtPrice.getText());
        double tax = 0.1;
        double total = quanlity * price * (1 + tax);

        NumberFormat nf = NumberFormat.getNumberInstance(lc);
        String sQuanlity = nf.format(quanlity);

        NumberFormat cf = NumberFormat.getCurrencyInstance(lc);
        String sPrice = cf.format(price);
        String sTotal = cf.format(total);

        NumberFormat pf = NumberFormat.getPercentInstance(lc);
        String sTax = pf.format(tax);

        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, lc);
        String sOrderDate = "";
        try {
            Date orderDate = df.parse(txtOrderDate.getText());

            df = DateFormat.getDateInstance(DateFormat.LONG, lc);
            sOrderDate = df.format(orderDate);
        } catch (ParseException ex) {
            Logger.getLogger(Order.class.getName()).log(Level.SEVERE, null, ex);
        }


        txaSummary.append("Date : " + sOrderDate + "\n");
        txaSummary.append("Product : " + txtProductName.getText() + "\n");
        txaSummary.append("Quanlity : " + txtQuanlity.getText() + "\n");
        txaSummary.append("Price : " + txtPrice.getText() + "\n");
        txaSummary.append("You have ordered "
                + sQuanlity + " "
                + txtProductName.getText()
                + " Price : "
                + sPrice
                + " VAT : " + sTax
                + ". Total : " + sTotal);
    }//GEN-LAST:event_btnPaymentActionPerformed

    private void cboLanguageItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboLanguageItemStateChanged
        if (evt.getStateChange() == evt.SELECTED) {
            changeLanguage();
        }
    }//GEN-LAST:event_cboLanguageItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Windows look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Windows (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Order.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Order().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPayment;
    private javax.swing.JComboBox cboLanguage;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblOrderDate;
    private javax.swing.JLabel lblPrice;
    private javax.swing.JLabel lblProductName;
    private javax.swing.JLabel lblQuanlity;
    private javax.swing.JTextArea txaSummary;
    private javax.swing.JTextField txtOrderDate;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtProductName;
    private javax.swing.JTextField txtQuanlity;
    // End of variables declaration//GEN-END:variables
}
